package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.pojo.AddressBook;
import com.itheima.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    @GetMapping("/list")
    public R<List<AddressBook>> list(HttpSession session) {
        log.info("查询当前用户所有地址");
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(AddressBook::getUserId, session.getAttribute("user"));
        List<AddressBook> list = addressBookService.list(addressBookLambdaQueryWrapper);
        return R.success(list);
    }

    /**
     * <p>Description: 添加地址</p>
     *
     * @author liuyiming
     * @date 2022-10-13 17:49
     * @version 1.0.0
     */
    @PostMapping
    public R<String> addAddress(HttpSession session, @RequestBody AddressBook address) {
        log.info("添加地址信息");
        Object userId = session.getAttribute("user");
        address.setUserId((Long) userId);
        addressBookService.save(address);
        return R.success("添加地址成功");
    }

    /**
     * <p>Description: 根据地址ID查询</p>
     *
     * @author liuyiming
     * @date 2022-10-13 18:51
     * @version 1.0.0
     */
    @GetMapping("/{id}")
    public R<AddressBook> queryById(@PathVariable Long id) {
        log.info("通过地址ID查询地址信息");
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }

    /**
     * <p>Description: 根据地址ID修改地址信息</p>
     *
     * @author liuyiming
     * @date 2022-10-13 19:16
     * @version 1.0.0
     */
    @PutMapping
    public R<String> updateById(@RequestBody AddressBook addressBook) {
        log.info("根据地址ID修改地址信息");
        addressBookService.updateById(addressBook);
        return R.success("修改地址成功");
    }

    /**
     * <p>Description: 设置默认地址</p>
     *
     * @author liuyiming
     * @date 2022-10-13 19:20
     * @version 1.0.0
     */
    @PutMapping("/default")
    @Transactional
    public R<String> defaultAddress(@RequestBody AddressBook addressBook) {
        log.info("设置默认地址");
        //1.根据当前登录的用户的id，把当前用的所有地址的默认值都设置为0
        LambdaUpdateWrapper<AddressBook> luw = new LambdaUpdateWrapper<>();
        luw.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        luw.set(AddressBook::getIsDefault, 0);
        addressBookService.update(luw);
        //2.通过前端传的地址id值，把这个地址的默认值修改为1
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("设置成功");
    }


    @GetMapping("/default")
    @Transactional
    public R<AddressBook> defaultAddress() {
        log.info("查询默认地址");
        //1.根据当前登录的用户的id，把当前用的所有地址的默认值都设置为0
        LambdaQueryWrapper<AddressBook> luw = new LambdaQueryWrapper<>();
        luw.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        luw.eq(AddressBook::getIsDefault, 1);

        AddressBook one = addressBookService.getOne(luw);
        return R.success(one);
    }


    /**
     * <p>Description: 根据ID删除地址</p>
     *
     * @author liuyiming
     * @date 2022-10-13 21:57
     * @version 1.0.0
     */
    @DeleteMapping
    public R<String> deleteById(Long ids) {
        log.info("根据ID删除地址信息");
        addressBookService.removeById(ids);
        return R.success("删除成功");
    }


}
